package com.situ.student.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.entity.Banji;
import com.situ.student.entity.Course;
import com.situ.student.entity.PageBean;
import com.situ.student.service.IBanjiService;
import com.situ.student.service.ICourseService;
import com.situ.student.service.impl.BanjiServiceImpl;
import com.situ.student.service.impl.CourseServiceImpl;

public class BanjiController extends HttpServlet {
	private IBanjiService banjiService = new BanjiServiceImpl();
	private ICourseService courseService = new CourseServiceImpl();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		/*post乱码问题，由Filter过滤器已解决
		 * request.setCharacterEncoding("UTF-8");
		 * */
		resp.setContentType("text/html;charset=utf-8");//服务器响应浏览器乱码问题
		String method = req.getParameter("method");
		switch (method) {
		//展示列表
		case "list":
			list(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
		case "deleteAll":
			deleteAll(req,resp);
			break;	
		case "insert":
			insert(req,resp);
			break;	
		case "toUpdate":
			toUpDate(req,resp);
			break;
		case "update":
			update(req,resp);
			break;	
		case "pageList":
			pageList(req,resp);
			break;	
			//ajax,使添加名字不重复	
		case "checkName":
			checkName(req, resp);
			break;	
		//多表查询	
		case "getAddPage":
			getAddPage(req, resp);
			break;			
		default:
			break;
		}
	}
	
	//多表查询
	private void getAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Course> list = courseService.list();
		req.setAttribute("list", list);
		req.getRequestDispatcher("/banji_add.jsp").forward(req, resp);
		
	}

	private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		boolean isExist =  banjiService.checkName(name);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write("{\"isExist\": "+isExist+"}");
		
	}

	/*分页*/
	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//接受请求参数，封装成对象
		String pageNoStr = req.getParameter("pageNo");
		if (pageNoStr == null || pageNoStr.equals("")) {
			pageNoStr = "1";
		}
		int pageNo = Integer.parseInt(pageNoStr);
		String pageSizeStr = req.getParameter("pageSize");
		if (pageSizeStr == null || pageSizeStr.equals("")) {
			pageSizeStr = "3";
		}
		int pageSize = Integer.parseInt(pageSizeStr);
		//封装为pageBean，调用业务逻辑
		PageBean<Map<String, Object>> pageBean = banjiService.getPagBean(pageNo,pageSize);
		System.out.println(pageBean);
		
		req.setAttribute("pageBean", pageBean);//展示的分页
		req.getRequestDispatcher("banji_list.jsp").forward(req, resp);
	}

	/*全部删除*/
	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String[] selectIds = req.getParameterValues("selectIds");//找到选中的值
		System.out.println(Arrays.toString(selectIds));
		boolean result = banjiService.deleteAll(selectIds);
		System.out.println(result ? "成功" : "失败");
		resp.sendRedirect(req.getContextPath() + "/banji?method=pageList");
		
	}

	/*修改数据*/
	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		/*得到修改后的值*/
		Integer id = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		Integer people = Integer.parseInt(req.getParameter("people"));
		String teatch = req.getParameter("teatch");
		Integer courseId = Integer.parseInt( req.getParameter("courseId"));
		Banji banji = new Banji(id, name, people, teatch, courseId, null);
		/*service方法*/
		boolean result = banjiService.update(banji);
		System.out.println(result ? "成功" : "失败");
		/*重定向*/
		resp.sendRedirect(req.getContextPath()+"/banji?method=pageList");
	}
	
	/*修改前，展示页面*/
	private void toUpDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/*找到要修改的对象的id*/
		Integer id = Integer.parseInt(req.getParameter("id"));
		/*service方法,拿到一组数据*/
		Banji banji = banjiService.findById(id);
		//显示课程列表
		List<Course> list = courseService.list();
		req.setAttribute("list", list);
		/*转发*/
		req.setAttribute("banji", banji);
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
	}
	
	/*添加*/
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	//得到浏览器的参数信息
		String name = req.getParameter("name");
		Integer people = Integer.parseInt(req.getParameter("people"));
		String teatch = req.getParameter("teatch");
		Integer courseId =Integer.parseInt(req.getParameter("courseId"));
		Banji banji = new Banji(name, people, teatch, courseId);
		//service方法
		boolean resule = banjiService.insert(banji);
		System.out.println(resule ? "成功" : "失败");
		//重定向
		resp.sendRedirect(req.getContextPath() +"/banji?method=pageList");
	}
	
	//根据id删除
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		/*1、先拿到浏览器传过来的id*/
		Integer id = Integer.parseInt(req.getParameter("id"));
		/*service层处理 ，返回来的ture，打印成功否则失败*/
		boolean result = banjiService.deleteById(id);
		System.out.println(result ? "成功" : "失败");
		/*重定向,删除完再回来*/
		resp.sendRedirect(req.getContextPath()+"/banji?method=pageList");
	}

	//展示
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Banji> list = new ArrayList<>();//创建数组
		list = banjiService.list();
		for (Banji banji : list) {
		System.out.println(banji);
		}
		req.setAttribute("list", list); //展示分页
		req.getRequestDispatcher("banji_list.jsp").forward(req, resp); //转发
	}

		
	}

